import {
  createRouter,
  // createWebHashHistory,
  createWebHistory
} from 'vue-router'
import { useUserStore } from '@/stores/index.js'

const router = createRouter({
  // import.meta.env.BASE_URL在vite.config.js中可以配置,key为 base
  history: createWebHistory(import.meta.env.BASE_URL),
  // 使用hash模式的url路径
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') },
    {
      path: '/',
      redirect: '/article/manage',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      children: [
        {
          path: '/article/manage',
          component: () => import('@/views/article/ArticleManage.vue')
        },
        {
          path: '/article/channel',
          component: () => import('@/views/article/ArticleChannel.vue')
        },
        {
          path: '/user/profile',
          component: () => import('@/views/user/UserProfile.vue')
        },
        {
          path: '/user/avatar',
          component: () => import('@/views/user/UserAvatar.vue')
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        }
      ]
    }
  ]
})
router.beforeEach(to => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') {
    // 将用户重定向到登录页面
    return '/login'
  }
})

export default router
